package tech.senri.iot.socket.service.socket

import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelFuture
import io.netty.channel.ChannelOption
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

@Component
class TCPServer
{
    private val log = LoggerFactory.getLogger(this.javaClass)

    @Value("\${nio.server.port:8999}")
    private var port = 8999

    /** server bootstrap  */
    private val bootstrap: ServerBootstrap by lazy { ServerBootstrap() }


    private val boss by lazy { NioEventLoopGroup(1) }


    private val worker by lazy { NioEventLoopGroup() }


    @Autowired
    private lateinit var serverChannelInitializer: ServerChannelInitializer


    fun start(): ChannelFuture?
    {
        bootstrap.group(boss, worker)
            .channel(NioServerSocketChannel::class.java)
            .childHandler(serverChannelInitializer)
            .option(ChannelOption.SO_BACKLOG, 1024)
            .childOption(ChannelOption.SO_KEEPALIVE, true)
        val channelFuture = bootstrap.bind(port).syncUninterruptibly()
        if (channelFuture.isSuccess)
        {
            log.info("socket 服务启动成功")
        }
        return channelFuture
    }
}
